package com.gamesnet.api;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.gamesnet.models.Commentaire;
import com.gamesnet.models.Console;
import com.gamesnet.models.Jeu;
import com.gamesnet.models.Message;
import com.gamesnet.models.Publication;
import com.gamesnet.models.Utilisateur;

public class Api {
	public static boolean Auth(String email, String password) throws JSONException{
		boolean result = false;
		Request req = new Request("auth");
		req.addParameters("password", password);
		req.addParameters("email", email);
		Utilisateur currentUser; 
		JSONObject json = req.send();
		Log.v("API", json.toString());
		if(null != json){
			result = json.getBoolean("response");

			if(result) {
				currentUser= new Utilisateur( json.getString("mail"), json.getString("name"),
						json.getString("firstname"),json.getString("avatar"),json.getString("address"),
						json.getString("zipcode"),json.getString("city"),json.getLong("id"));

				Session.getInstance().setUser(currentUser);
				return true;
			}

		}
		return result;
	}

	public static Utilisateur getUser(String userId) throws JSONException{
		boolean result = false;

		Request req = new Request("user/getuser");
		req.addParameters("userId", userId);
		JSONObject json = req.send();
		Log.v("API get user", json.toString());
		if(null != json){
			result = json.getBoolean("success");
			if(result) {
				return new Utilisateur( json.getString("mail"), json.getString("name"),
						json.getString("firstname"),json.getString("avatar"),json.getString("address"),
						json.getString("zipcode"),json.getString("city"),json.getLong("id"));
			}

		}
		return null;
	}


	public static List<Console> consoles(String id)throws JSONException{
		boolean result = false;
		Request req = new Request("userconsoles/getconsoles");
		req.addParameters("userId", id);

		JSONObject json = req.send();
		Log.v("API", json.toString());

		if(null != json){
			result = json.getBoolean("succes");
			List<Console> consoles = new ArrayList<Console>();

			if(result) {
				JSONArray jsonArrayConsole = json.getJSONArray("consoles");
				for(int i = 0; i<jsonArrayConsole.length();i++){
					JSONObject jsonConsole = jsonArrayConsole.getJSONObject(i);
					consoles.add(
							new Console(jsonConsole.getLong("id"), jsonConsole.getString("name"), jsonConsole.getString("image")));
				}
				return consoles;

			}
		}
		return null;
	}

	public static Console console(String consoleId) throws JSONException{

		boolean result = false;
		Request req = new Request("console/maconsole");
		req.addParameters("id", consoleId);

		JSONObject json = req.send();
		Log.v("API get console", json.toString());

		if(null != json){
			result = json.getBoolean("succes");
			if(result) {
				JSONObject infoConsole = json.getJSONObject("console");
				return new Console(infoConsole.getLong("id"), infoConsole.getString("name"), 
						infoConsole.getString("description"), infoConsole.getString("image"));
			}

		}

		return null;

	}

	public static List<Jeu> jeux(String id)throws JSONException{
		boolean result = false;
		Request req = new Request("game/gamebyuser");
		req.addParameters("id", id);

		JSONObject json = req.send();
		Log.v("API", json.toString());

		if(null != json){
			result = json.getBoolean("succes");
			List<Jeu> jeux = new ArrayList<Jeu>();

			if(result) {
				JSONArray jsonArrayJeux = json.getJSONArray("games");
				for(int i = 0; i<jsonArrayJeux.length();i++){
					JSONObject jsonJeu = jsonArrayJeux.getJSONObject(i);
					jeux.add(
							new Jeu(jsonJeu.getLong("id"), jsonJeu.getString("name"), jsonJeu.getString("image")));
				}
				return jeux;

			}
		}
		return null;
	}

	public static Jeu jeu(String jeuId)throws JSONException{

		boolean result = false;
		Request req = new Request("game/onegame");
		req.addParameters("id", jeuId);

		JSONObject json = req.send();
		Log.v("API get jeu", json.toString());

		if(null != json){
			result = json.getBoolean("succes");
			if(result) {
				JSONObject infoJeu = json.getJSONObject("game");
				Console c = Api.console(infoJeu.getString("console"));
				return new Jeu(infoJeu.getLong("id"), infoJeu.getString("name"), 
						infoJeu.getString("description"), infoJeu.getString("picture"),c);
			}

		}
		return null;

	}

	public static List<Message>messagesRecu(String userId, String offset,String limit)throws JSONException{
		boolean result = false;
		Request req = new Request("message/receivedmessages");
		req.addParameters("limit", limit);
		req.addParameters("offset", offset);
		req.addParameters("userid", userId);

		JSONObject json = req.send();
		Log.v("API get message", json.toString());

		if(null != json){
			result = json.getBoolean("success");
			List<Message> messages = new ArrayList<Message>();
			if(result) {
				JSONArray jsonArrayMessages = json.getJSONArray("content");
				for(int i = 0; i<jsonArrayMessages.length(); i++){
					JSONObject jsonMessage = jsonArrayMessages.getJSONObject(i).getJSONObject("message");
					JSONObject jsonUserMessage = jsonArrayMessages.getJSONObject(i).getJSONObject("userMessage");
					Utilisateur expediteur = Api.getUser(jsonMessage.getString("sender"));
					messages.add(new Message(jsonMessage.getLong("id"),jsonMessage.getString("object"), 
							expediteur, jsonMessage.getString("dateCreated"), jsonUserMessage.getBoolean("wasRead")));
				}
				return messages;
			}
		}

		return null;

	}
	
	public static List<Message>messagesEnvoyes(String userId, String offset,String limit)throws JSONException{
		boolean result = false;
		Request req = new Request("message/sentmessages");
		req.addParameters("limit", limit);
		req.addParameters("offset", offset);
		req.addParameters("userid", userId);

		JSONObject json = req.send();
		Log.v("API get message", json.toString());

		if(null != json){
			result = json.getBoolean("success");
			List<Message> messages = new ArrayList<Message>();
			if(result) {
				JSONArray jsonArrayMessages = json.getJSONArray("content");
				for(int i = 0; i<jsonArrayMessages.length(); i++){
					JSONObject jsonMessage = jsonArrayMessages.getJSONObject(i).getJSONObject("message");
					JSONObject jsonUserMessage = jsonArrayMessages.getJSONObject(i).getJSONObject("userMessage");
					Utilisateur destinataire = Api.getUser(jsonUserMessage.getString("sendee"));
					messages.add(new Message(jsonMessage.getLong("id"),jsonMessage.getString("object"), 
							 jsonMessage.getString("dateCreated"), destinataire));
				}
				return messages;
			}
		}

		return null;

	}
	
	public static Message messageRecu(String messageId) throws JSONException{
		boolean result = false;
		Request req = new Request("message/read");
		req.addParameters("messagId", messageId);
		
		JSONObject json = req.send();
		Log.v("API get message", json.toString());

		if(null != json){
			result = json.getBoolean("success");
			if(result){
				Utilisateur expediteur = Api.getUser(json.getString("sender"));
				return new Message(expediteur, json.getLong("id"), json.getString("object"), json.getString("content"));
			}
			
		}
		return null;
		
	}
	
	public static Message messageEnvoye(String messageId, String userId) throws JSONException{
		boolean result = false;
		Request req = new Request("message/readsendmessage");
		req.addParameters("messagId", messageId);
		req.addParameters("userId", userId);
		
		JSONObject json = req.send();
		Log.v("API get message", json.toString());

		if(null != json){
			result = json.getBoolean("success");
			if(result){
				Utilisateur destinataire = Api.getUser(json.getString("sendee"));
				return new Message(json.getLong("id"), destinataire, json.getString("object"), json.getString("content"));
			}
			
		}
		return null;
		
	}
	
	public static List<Publication>wall(String userId, String offset) throws JSONException{
		boolean result = false;
		Request req = new Request("post/getuserposts");
		req.addParameters("offset", offset);
		req.addParameters("userid", userId);

		JSONObject json = req.send();
		Log.v("API get message", json.toString());

		if(null != json){
			result = json.getBoolean("success");
			List<Publication> publications = new ArrayList<Publication>();
			if(result) {
				JSONArray jsonArrayMessages = json.getJSONArray("posts");
				for(int i = 0; i<jsonArrayMessages.length(); i++){
					JSONObject jsonPublication = jsonArrayMessages.getJSONObject(i);
					Utilisateur expediteur = Api.getUser(jsonPublication.getString("sender"));
					int nbComments = Api.getNbComments(jsonPublication.getString("id"));
					publications.add(new Publication(jsonPublication.getLong("id"),jsonPublication.getString("content"), 
							jsonPublication.getString("dateCreated"), expediteur, nbComments));
				}
				return publications;
			}
		}

		return null;
	}
	
	public static List<Publication>actu(String userId) throws JSONException{
		boolean result = false;
		Request req = new Request("post/gethomeposts");
		req.addParameters("userid", userId);

		JSONObject json = req.send();
		Log.v("API get post", json.toString());

		if(null != json){
			result = json.getBoolean("success");
			List<Publication> publications = new ArrayList<Publication>();
			if(result) {
				JSONArray jsonArrayMessages = json.getJSONArray("posts");
				for(int i = 0; i<jsonArrayMessages.length(); i++){
					JSONObject jsonPublication = jsonArrayMessages.getJSONObject(i);
					Utilisateur expediteur = Api.getUser(jsonPublication.getString("sender"));
					int nbComments = Api.getNbComments(jsonPublication.getString("id"));
					publications.add(new Publication(jsonPublication.getLong("id"),jsonPublication.getString("content"), 
							jsonPublication.getString("dateCreated"), expediteur, nbComments));
				}
				return publications;
			}
		}

		return null;
	}
	
	public static int getNbComments(String postId)throws JSONException{
		boolean result = false;
		Request req = new Request("post/getpostnbcomments");
		req.addParameters("postid", postId);

		JSONObject json = req.send();
		Log.v("API get message", json.toString());

		if(null != json){
			result = json.getBoolean("success");
			if(result) {
				return json.getInt("nbComments");
			}
		}
		return 0;
	}
	
	public static Publication publication(String postId)throws JSONException{
		boolean result = false;
		Request req = new Request("post/getpost");
		req.addParameters("postid", postId);
		
		JSONObject json = req.send();
		Log.v("API get message", json.toString());

		if(null != json){
			result = json.getBoolean("success");
			if(result){
				Utilisateur expediteur = Api.getUser(json.getString("sender"));
				List<Commentaire> commentaires = Api.commentaires(postId);
				return new Publication(json.getLong("id"),json.getString("content"), 
						json.getString("dateCreated"), expediteur, commentaires);
			}
			
		}
		return null;
	}
	
	public static List<Commentaire> commentaires(String postId)throws JSONException{
		boolean result = false;
		Request req = new Request("post/getpostcomments");
		req.addParameters("postid", postId);

		JSONObject json = req.send();
		Log.v("API get post", json.toString());

		if(null != json){
			result = json.getBoolean("success");
			List<Commentaire> commentaires = new ArrayList<Commentaire>();
			if(result) {
				JSONArray jsonArrayCommentaire = json.getJSONArray("comments");
				for(int i = 0; i<jsonArrayCommentaire.length(); i++){
					JSONObject jsonPublication = jsonArrayCommentaire.getJSONObject(i);
					Utilisateur expediteur = Api.getUser(jsonPublication.getString("sender"));
					commentaires.add(new Commentaire(jsonPublication.getLong("id"),jsonPublication.getString("content"), 
							jsonPublication.getString("dateCreated"), expediteur));
				}
				return commentaires;
			}
		}

		return null;
	}
	
	public static List<Utilisateur>getFriends(String userId) throws JSONException{
		boolean result = false;
		Request req = new Request("friends/get");
		req.addParameters("userid", userId);

		JSONObject json = req.send();
		Log.v("API get friends", json.toString());

		if(null != json){
			result = json.getBoolean("succes");
			List<Utilisateur> amis = new ArrayList<Utilisateur>();
			if(result) {
				JSONArray jsonArrayAmis = json.getJSONArray("friends");
				for(int i = 0; i<jsonArrayAmis.length(); i++){
					JSONObject jsonAmis = jsonArrayAmis.getJSONObject(i);
					amis.add(new Utilisateur( jsonAmis.getString("mail"), jsonAmis.getString("name"),
							jsonAmis.getString("firstname"),jsonAmis.getString("avatar"),jsonAmis.getString("address"),
							jsonAmis.getString("zipcode"),jsonAmis.getString("city"),jsonAmis.getLong("id")));
				}
				return amis;
			}
		}

		return null;
	}
	
	public static List<Utilisateur>getSuggestions(String userId) throws JSONException{
		boolean result = false;
		Request req = new Request("friends/suggestions");
		req.addParameters("userid", userId);

		JSONObject json = req.send();
		Log.v("API get friends", json.toString());

		if(null != json){
			result = json.getBoolean("succes");
			List<Utilisateur> amis = new ArrayList<Utilisateur>();
			if(result) {
				JSONArray jsonArrayAmis = json.getJSONArray("friends");
				for(int i = 0; i<jsonArrayAmis.length(); i++){
					JSONObject jsonAmis = jsonArrayAmis.getJSONObject(i);
					amis.add(new Utilisateur( jsonAmis.getString("mail"), jsonAmis.getString("name"),
							jsonAmis.getString("firstname"),jsonAmis.getString("avatar"),jsonAmis.getString("address"),
							jsonAmis.getString("zipcode"),jsonAmis.getString("city"),jsonAmis.getLong("id")));
				}
				return amis;
			}
		}

		return null;
	}




}
